import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  //重定向
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: () => import('../views/Home'),
    children: [
      {
        path: '/home',
        redirect: '/home/movies'
      },
      {
        path: '/home/movies',
        component: () => import('../views/home/Movies'),
        children: [
          {
            path: '/home/movies',
            redirect: '/home/movies/hot',
          },
          {
            path: '/home/movies/hot',
            component: () => import('../views/home/movies/MyMain'),
            meta: {
              title: "猫眼热映",
            }
          },
          {
            path: '/home/movies/cinema',
            component: () => import('../views/home/movies/Cinema'),
            meta: {
              title: "猫眼影院",
            }
          },
          {
            path: '/home/movies/wait',
            component: () => import('../views/home/movies/Wait'),
            meta: {
              title: "猫眼待映",
            }
          },
          {
            path: '/home/movies/classical',
            component: () => import('../views/home/movies/Classical'),
            meta: {
              title: "猫眼经典",
            }
          },
        ]
      },
      {
        path: '/home/video',
        component: () => import('../views/home/Video'),
        meta: {
          title: "猫眼视频",
        }
      },
      {
        path: '/home/mini',
        component: () => import('../views/home/Mini')
      },
      {
        path: '/home/show',
        component: () => import('../views/home/Show')
      },
      {
        path: '/home/mine',
        component: () => import('../views/home/Mine'),
        meta: {
          requireLogin: true,

        },
        // beforeEach: (to, from, next) => {
        //   console.log(1);
        //   // 路由鉴权
        //   if (localStorage.getItem("token")) {
        //     next();
        //   } else {
        //     next('/login');
        //   }
        // }//

      },
    ]
  },
  {
    path: '/city',
    component: () => import('../views/City')
  },
  {
    path: '/detail/:id',
    props: true,
    component: () => import('../views/Detail'),
    meta: {
      title: "猫眼详情",
    }
  },
  {
    path: '/search',
    component: () => import('../views/Search')
  },
  {
    path: '/login',
    component: () => import('../views/Login')
  },
  {
    path: '*',
    component: () => import('../views/NotFound')
  },
];

const router = new VueRouter({
  routes,
});

// 全局前置守卫
router.beforeEach((to, from, next) => {

  // 路由鉴权  to.meta.requireLogin
  if (to.meta.requireLogin) {
    if (localStorage.getItem("token")) {
      next();
    } else {
      next('/login');
    }

  } else {
    next();
  }

  document.title = to.meta.title || "猫眼-娱乐看猫眼";
  next();
})
export default router;